package com.tensquare.qa.service;

import com.tensquare.qa.dao.ProblemDao;
import com.tensquare.qa.pojo.Problem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.swing.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:zimmor
 * @Date: 2021/3/1 14:17
 * @Description 提问业务逻辑层
 * @version: 1.0.0
 */
@Service
public class ProblemService {
    @Autowired
    private ProblemDao problemDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 获取等待回答问题列表
     * @param labelid
     * @param page
     * @param size
     * @return
     */
    public Page<Problem> waitList(String labelid, int page, int size) {
        Pageable pageable = PageRequest.of(page-1, size);
        return problemDao.waitList(labelid, pageable);
    }
    /**
     * 获取最热问题列表
     * @param labelId
     * @param page
     * @param size
     * @return
     */
    public Page<Problem> hotList(String labelId, int page, int size) {
        Pageable pageable = PageRequest.of(page-1, size);
        return problemDao.hotList(labelId, pageable);
    }

    /**
     * 根据labelid获取最新问题列表
     * @param labelid
     * @param page
     * @param size
     * @return
     */
    public Page<Problem> newProblem(String labelid, int page, int size) {
        Pageable pageable = PageRequest.of(page-1, size);
        return problemDao.newList(labelid, pageable);
    }
    /**
     * 分页查询
     * @param problem
     * @param page
     * @param size
     * @return
     */
    public Page<Problem> findSearch(Problem problem, int page, int size) {
        Pageable pageable = PageRequest.of(page-1, size);
        Specification<Problem> specification = new Specification<Problem>() {
            @Override
            public Predicate toPredicate(Root<Problem> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<>();
                if(problem.getContent()!=null && !"".equals(problem.getContent())) {
                    Predicate content = criteriaBuilder.like(root.get("content").as(String.class), "%" + problem.getContent() + "%");
                    list.add(content);
                }
                if(problem.getTitle()!=null && !"".equals(problem.getTitle())) {
                    Predicate title = criteriaBuilder.like(root.get("title").as(String.class), "%" + problem.getTitle() + "%");
                    list.add(title);
                }
                Predicate[] predicates = new Predicate[list.size()];
                list.toArray(predicates);
                return criteriaBuilder.and(predicates);
            }
        };
        return problemDao.findAll(specification, pageable);
    }

    /**
     * 条件查询
     * @param problem
     * @return
     */
    public List<Problem> findSearch(Problem problem) {
        return problemDao.findAll(new Specification<Problem>() {
            @Override
            public Predicate toPredicate(Root<Problem> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<>();
                if(problem.getContent()!=null && !"".equals(problem.getContent())) {
                    Predicate contemt = criteriaBuilder.like(root.get("contemt").as(String.class), "%" + problem.getContent() + "%");
                    list.add(contemt);
                }
                if(problem.getTitle()!=null && !"".equals(problem.getTitle())) {
                    Predicate title = criteriaBuilder.like(root.get("title").as(String.class), "%" + problem.getTitle() + "%");
                    list.add(title);
                }
                Predicate[] predicates = new Predicate[list.size()];
                list.toArray(predicates);
                return criteriaBuilder.and(predicates);
            }
        });
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public Problem findById(String id) {
        return problemDao.findById(id).get();
    }

    /**
     * 新增
     * @param problem
     */
    public void add(Problem problem) {
        problem.setId(idWorker.nextId()+"");
        problemDao.save(problem);
    }

    /**
     * 修改
     * @param problem
     */
    public void update(Problem problem) {
        problemDao.save(problem);
    }

    /**
     * 删除
     * @param id
     */
    public void deleteById(String id ) {
        problemDao.deleteById(id);
    }
}
